const BaseController = require('./base');

class BankController extends BaseController {
  async addTag() {
    const { ctx } = this;
    ctx.validate({
      name: { type: 'string' },
      color: { type: 'string' },
    });
    const tagInfo = ctx.request.body;
    const res = await ctx.service.bank.addTag(tagInfo);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async updateTag() {
    const { ctx } = this;
    ctx.validate({
      id: { type: 'string' },
      name: { type: 'string' },
      color: { type: 'string' },
    });
    const tagInfo = ctx.request.body;
    const res = await ctx.service.bank.updateTag(tagInfo);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async getTags() {
    const { ctx } = this;
    const { id, pageNum, pageSize, name = '' } = ctx.query;
    if (id) {
      const res = await ctx.service.bank.getTag(ctx.query.id);
      if (res) {
        this.success(res);
      } else {
        this.fail('标签不存在');
      }
    } else {
      const res = await ctx.service.bank.getTags(Number(pageNum), Number(pageSize), name);
      if (res) {
        this.success(res);
      } else {
        this.fail('查询失败');
      }
    }
  }

  async delTags() {
    const { ctx } = this;
    const ids = ctx.request.body;
    if (!ids || !(ids instanceof Array)) {
      this.fail('参数错误');
      return;
    }
    const res = await ctx.service.bank.delTags(ids);
    if (res) {
      this.success('删除成功');
    } else {
      this.fail('删除失败');
    }
  }

  async addQuesType() {
    const { ctx } = this;
    ctx.validate({
      name: { type: 'string' },
      data: { type: 'array' },
    });
    const quesTypeInfo = ctx.request.body;
    const result = await ctx.service.bank.addQuesType(quesTypeInfo);
    if (result) {
      this.success('添加成功');
    } else {
      this.fail('添加失败');
    }
  }

  async updateQuesType() {
    const { ctx } = this;
    ctx.validate({
      id: { type: 'string' },
      name: { type: 'string' },
      data: { type: 'array' },
    });
    const quesTypeInfo = ctx.request.body;
    const result = await ctx.service.bank.updateQuesType(quesTypeInfo);
    if (result) {
      return this.success('修改成功');
    }
    return this.fail('修改失败');
  }

  async getQuseType() {
    const { ctx } = this;
    const { id, pageNum, pageSize, name = '' } = ctx.query;
    if (id) {
      const res = await ctx.service.bank.getQuseType(ctx.query.id);
      if (res) {
        this.success(res);
      } else {
        this.fail('题型不存在');
      }
    } else {
      const res = await ctx.service.bank.getQuseTypes(Number(pageNum), Number(pageSize), name);
      if (res) {
        this.success(res);
      } else {
        this.fail('查询失败');
      }
    }
  }

  async delQuesType() {
    const { ctx } = this;
    const ids = ctx.request.body;
    if (!ids || !(ids instanceof Array)) {
      this.fail('参数错误');
      return;
    }
    const result = await ctx.service.bank.delQuesType(ids);
    if (result.state) {
      this.success(result.msg);
    } else {
      this.fail(result.msg);
    }
  }

  async addQues() {
    const { ctx } = this;
    ctx.validate({
      data: { type: 'renderList' },
      type: { type: 'string' },
    });
    const quesInfo = ctx.request.body;
    const result = await ctx.service.bank.addQues(quesInfo);
    if (result.state) {
      this.success(result.msg);
    } else {
      this.fail(result.msg);
    }
  }

  async delQues() {
    const { ctx } = this;
    const ids = ctx.request.body;
    if (!ids || !(ids instanceof Array)) {
      this.fail('参数错误');
      return;
    }
    const result = await ctx.service.bank.delQues(ids);
    if (result) {
      this.success('删除成功');
    } else {
      this.fail('删除失败');
    }
  }

  async updateQues() {
    const { ctx } = this;
    ctx.validate({
      id: { type: 'string' },
      data: { type: 'renderList' },
      type: { type: 'string' },
    });
    const quesInfo = ctx.request.body;
    const res = await ctx.service.bank.updateQues(quesInfo);
    if (res.state) {
      this.success(res.msg);
    } else {
      this.fail(res.msg);
    }
  }

  async getQues() {
    const { ctx } = this;
    const { id, pageNum, pageSize, title = '' } = ctx.query;
    if (id) {
      const res = await ctx.service.bank.getQues(id);
      if (res) {
        this.success(res);
      } else {
        this.fail('题目不存在');
      }
    } else {
      const res = await ctx.service.bank.getQuess(Number(pageNum), Number(pageSize), title);
      if (res) {
        this.success(res);
      } else {
        this.fail('查询失败');
      }
    }
  }
}

module.exports = BankController;
